

import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;

public class ControlPanelListener
	implements ActionListener, ItemListener {

  protected DrawingCanvas canvas;
  protected LinkedList ShapeObjects;

  ControlPanelListener(DrawingCanvas canvas, LinkedList<Tool> ShapeObjects)  {
    this.canvas=canvas;
    this.ShapeObjects = ShapeObjects;
  }

  public void actionPerformed(ActionEvent e) {
    canvas.clearCanvas();
    if(e.getActionCommand().compareTo("Delete Selected Object")==0){
    	if(canvas.getCurrentTool().getClass().toString().contains("SelectTool")){
        	ShapeObjects.remove(((SelectTool)canvas.getCurrentTool()).sobj);
        	((SelectTool)canvas.getCurrentTool()).sobj = null;
        	
        }
//    	canvas.getCurrentTool().paintObjects(0);
    }else if(e.getActionCommand().compareTo("Clear")==0){
    	ShapeObjects.clear();
    	canvas.getCurrentTool().sobj = null;
    }
    canvas.repaint();
    canvas.getCurrentTool().paintObjects(0);
  }

  public void itemStateChanged(ItemEvent e)  {
    if (e.getStateChange() == ItemEvent.SELECTED) {
      canvas.setPenColor(itemToColor(e.getItem()));
    }
    if(canvas.getCurrentTool().getClass().toString().contains("SelectTool")){
    	((SelectTool)canvas.getCurrentTool()).sobj.setColor(itemToColor(e.getItem()));
    	((SelectTool)canvas.getCurrentTool()).paintObjects(0);
    	((SelectTool)canvas.getCurrentTool()).sobj.selectObject(canvas);
    }
  }

  protected Color itemToColor(Object item) {
    
    if("black".equals(item)) {
      return Color.black;
    }
    else if("blue".equals(item)) {
          return Color.blue;
         }
         else if("green".equals(item)) {
               return Color.green;
              }
              else {
                return Color.red;
              }
  }

} 
